const { conn } = require('./mysql')

console.log(conn)

function queryAll() {
    // conn.query('select * from user_tel', function (error, results, fields) {
    //         if (error) throw error;
    //         console.log(results);
    //   });
    return new Promise((resolve, reject) => {
        // 执行一个查询
        conn.query('select * from user_tel', function (error, results) {
            if (error) reject(error)
            resolve(results)
        });
    })
}
function queryAllByPage(obj) {
    return new Promise((resolve, reject) => {
        // 执行一个查询/
        conn.query('select *,count(1) over() total from user_tel limit ?,?',[(obj.pageNo-1)*obj.pageSize,obj.pageSize], function (error, results) {
            if (error) reject(error)
            resolve(results)
        });
    })
}


function queryAllDemo2(obj) {
    return new Promise((resolve, reject) => {
        // 执行一个查询
        conn.query('select id,username,tel,create_time,count(1) over(partition by username) userbuycount,min(create_time) over(partition by username order by create_time) firstbuytime,row_number() over(partition by username order by create_time) rn,count(1) over() total from user_tel limit ?,?',[(obj.pageNo-1)*obj.pageSize,obj.pageSize], function (error, results) {
            if (error) reject(error)
            resolve(results)
        });
    })
}

function queryById(id) {


    return new Promise((resolve, reject) => {
        // 执行一个查询
        conn.query('select * from user_tel where id = ?', [id], function (error, resultss) {
            if (error) reject(error)
            resolve(results)
        });
    })
}

//queryById(1);
//queryAll();
module.exports = {
    queryAll, queryById,queryAllDemo2,queryAllByPage
}
